/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reyes.mysite.servlet;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import reyes.mysite.jdo.Category;
import reyes.mysite.jdo.Blog;
import reyes.mysite.dao.GenericDAO;
import org.json.JSONArray;
import org.json.JSONObject;
import reyes.mysite.helper.MethodHelper;

/**
 *
 * @author reyesyang
 */
public class BlogServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");

        try {
            String task = request.getParameter("task");
            if (task.equals("createCategory")) {
                createCategory(request, response);
            } else if (task.equals("deleteCategory")) {
                deleteCategory(request, response);
            } else if (task.equals("deleteBlog")) {
                deleteBlog(request, response);
            } 

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public void createCategory(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String categoryName = request.getParameter("category");
        GenericDAO<Category> categoryDAO = new GenericDAO<Category>();
        String queryString = "select from " + Category.class.getName() + " where name == '" + categoryName + "'";

        JSONObject json = new JSONObject();

        try {
            List<Category> exists = categoryDAO.query(queryString);
            if (!exists.isEmpty()) {
                json.put("exist", "true");
            } else {
                Category category = new Category(categoryName);
                categoryDAO.save(category);
                json = new MethodHelper().getAllCategoriesJSON();
            }
            out.append(json.toString());
        } catch (Exception e) {
            System.out.println("Can't create category: " + categoryName + ", Details: " + e.getMessage());
        } finally {
            categoryDAO.closePM();
            out.close();
        }

        //Get max index from database
    }

    public void deleteCategory(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String nickName = request.getParameter("nickName");
        GenericDAO<Category> categoryDAO = new GenericDAO<Category>();
        String queryString = "select from " + Category.class.getName() + " where nickName =='" + nickName + "'";

        try {
            categoryDAO.delete(queryString);
            JSONObject json = new MethodHelper().getAllCategoriesJSON();
            out.append(json.toString());
        } catch (Exception e) {
            System.out.println("Can't delete category, Details: " + e.getMessage());
        } finally {
            categoryDAO.closePM();
            out.close();
        }
    }

    private void deleteBlog(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String id = request.getParameter("id");

        GenericDAO<Blog> blogDAO = new GenericDAO<Blog>();
        Blog blog = blogDAO.getById(Blog.class, Long.valueOf(id));

        try {
            blogDAO.delete(blog);
        } catch (Exception e) {
            System.out.println(
                    MessageFormat.format("Can't delete blog {0}, Details: {1}",
                    blog.getTitle(),
                    e.getMessage()));
        } finally {
            blogDAO.closePM();
        }
    }
}
